#!/bin/bash

# 获取当前日期和时间戳作为文件名
current_date=$(date "+%Y-%m-%d")
specified_date="$1" # 命令行参数 可传递2023-12-08
timestamp="$specified_date-commit-log"
filename="/d/Desktop/小收纳间/git-usage-guide/${timestamp}.txt"

# 创建文件
touch "$filename"

# 获取仓库列表
repos=(
  "/c/Users/FengYi/Documents/Arduino"
  "/d/Desktop/小收纳间/chassis_ironCamel"
  "/d/Desktop/小收纳间/git-usage-guide"
  "/d/Desktop/小收纳间/IronCamel3"
  "/d/Desktop/小收纳间/IronCamelManager"
  "/d/Desktop/小收纳间/PythonMagic"
  "/d/Desktop/小收纳间/realsite"
  "/d/Desktop/小收纳间/远程TT0001"
  "/d/Desktop/小收纳间/远程TT0002"
  "/d/Desktop/小收纳间/远程TT0003"
  "/d/Desktop/小收纳间/FSC"
  "/d/Desktop/小收纳间/群发短信测试"
  "/d/Desktop/小收纳间/TT0003-2023-archive"
  "/d/Desktop/小收纳间/生产环境TT0003"
)
=
# 遍历每个仓库
for repo in "${repos[@]}"; do
  echo "Repository: $repo" >> "$filename"
  
  # 进入仓库目录
  cd "$repo"
  
  # 该commit的都commit上去
  git add .
  git commit -m "creating weekly log"

  # 获取当前分支
  current_branch=$(git symbolic-ref --short HEAD)

  # 遍历每个分支：列出所有远程分支，去除输出中每行的前导空格，忽略带有 -> 的行（grep -v是反向匹配）
  branches=$(git branch -r | sed 's/^[[:space:]]*//' | grep -v '\->') 
  for branch in $branches; do
    echo "Branch: $branch" >> "$filename"
    # 切换到分支：${branch#*/} 用于截取变量 branch 的值，去除其开头的斜杠 (/) 及其之前的部分
    git checkout "${branch#*/}"
    
    # 获取昨天的 Git 日志，并追加到文件中
    git log --since="$specified_date 00:00:00" --until="$specified_date 23:59:59" --pretty=format:"git commit -m %s" >> "$filename"
    
    echo >> "$filename"
  done
  
  echo >> "$filename"
  echo >> "$filename"

  # 最后回到原分支
  git checkout "${current_branch#*/}"
done